#!/bin/bash
# Debug script; builds the library, uninstalls it and then installs it

function ERROR
{
	echo "{ ERROR }"
	echo "Expected ./utility [ACTION]"
	echo "  statistics [generates statistics about Temelia]"
	
	# Shared object
	echo "  so_build [builds libtemelia.so]"
	echo "  so_clean [cleans object files and deletes libtemelia.so]"
	echo "  so_install [copies headers in /usr/include/temelia and libtemelia.so in /usr/lib]"
	echo "  so_uninstall [removes directory /usr/include/temelia and /usr/lib/libtemelia.so]"
	
	# DLL
	echo "  dll_build [builds temelia.dll]"
	echo "  dll_clean [cleans object files and deletes temelia.dll]"
	echo "  dll_install [copies headers in /usr/include/temelia and temelia.dll in /usr/lib]"
	echo "  dll_uninstall [removes directory /usr/include/temelia and /usr/lib/temelia.dll]"
}

TEMELIA="."
TEMELIA_SAMPLES="../temelia_samples"
TEMELIA_PERFORMANCE="../temelia_performance"
TEMELIA_PERFORMANCE_GRAPHS="../temelia_performance_graphs"
# WARNING - messing with these may kill your cat (and you system)
SRCDIR="src"
HDRDIR="$SRCDIR/include"
HDRS="$HDRDIR/*.h"
PRJNAME="temelia"
LOCLIBDIR="/usr/lib"
LOCINCDIR="/usr/include/$PRJNAME"

C_SOURCE_FILES="src/*.c"
C_HEADER_FILES="src/include/*.h"
CPP_SOURCE_FILES="src/*.cpp"
CPP_HEADER_FILES="src/include/*.hpp"
JAVA_FILES="src/org/ceata/temelia/*.java"

if [ $# -eq 0 ] ;
then
	ERROR
	exit
fi

if [ $1 == "statistics" ] ;
then
    total=0
    size=3
    query[0]=`wc -l $TEMELIA/$C_SOURCE_FILES $TEMELIA/$C_HEADER_FILES | grep total | cut -d " " -f 2`
    query[1]=`wc -l $TEMELIA_SAMPLES/$C_SOURCE_FILES $TEMELIA_SAMPLES/$C_HEADER_FILES | grep total | cut -d " " -f 3`
    query[2]=`wc -l $TEMELIA_PERFORMANCE/$CPP_SOURCE_FILES $TEMELIA_PERFORMANCE/$CPP_HEADER_FILES | grep total | cut -d " " -f 2`
    query[3]=`wc -l $TEMELIA_PERFORMANCE_GRAPHS/$JAVA_FILES | grep total | cut -d " " -f 2`

	echo "[temelia] "${query[0]}
	echo "[temelia samples] "${query[1]}
    echo "[temelia performance] "${query[2]}
	echo "[temelia performance graphs] "${query[3]}

	for i in `seq 0 $size`
    do
        total=`expr ${total} + ${query[$i]}`
    done
    echo "Temelia project: " $total

elif [ $1 == "so_build" ];
then
	make
elif [ $1 == "so_install" ];
then
	make install
elif [ $1 == "so_uninstall" ];
then
	make uninstall
elif [ $1 == "so_clean" ];
then
	make clean
elif [ $1 == "dll_build" ];
then
    make clean
	make CROSS_COMPILE=i586-mingw32msvc- dll
elif [ $1 == "dll_install" ];
then
	mkdir -p $LOCINCDIR
	cp $HDRS $LOCINCDIR
	cp temelia.dll $LOCLIBDIR
elif [ $1 == "dll_uninstall" ];
then
	rm -rf $LOCINCDIR
	rm -f $LOCLIBDIR/temelia.dll 
elif [ $1 == "dll_clean" ];
then
    make dll_clean
else
	ERROR
fi
